********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

/// MERGING ALL DATA SETS ///


// Combine Teacher and Director survey and then match with games


// Start with teacher survey that then will be saved and appended to director

	use "$Endline_dtRaw/_intermediate files/teachers.dta", clear


// Variables that need to be renamed

	// Section 1

	rename T01_01 TD_age
	rename T01_02 TD_female
	rename T01_03 TD_home_loc
	rename T01_03_other2 TD_home_other2
	rename T01_03_other3 TD_home_other3
	rename T01_04 TD_origin
	rename T01_04_other2 TD_origin_other2
	rename T01_04_other3 TD_origin_other3
	rename T01_05 TD_home_years
	rename T01_06 TD_edu
	rename T01_07__1 TD_edu_edu
	rename T01_07__2 TD_edu_peda
	rename T01_07__3 TD_edu_soc
	rename T01_07__4 TD_edu_apl
	rename T01_07_other TD_edu_spec
	rename T01_08 T_ocu_teach
	rename T01_09 TD_ocu
	rename T01_10 TD_marital
	rename T01_11 TD_edu_partner
	rename T01_12 TD_ocu_partner
	rename T01_13 TD_home_type
	rename T01_14 TD_income
	rename T01_15 TD_children
	rename T01_16 TD_children_others
	rename T01_17 TD_children_girls
	rename T01_18 TD_children_school
	rename T01_19__0 TD_children_pre
	rename T01_19__1 TD_children_1
	rename T01_19__2 TD_children_2
	rename T01_19__3 TD_children_3
	rename T01_19__4 TD_children_4
	rename T01_19__5 TD_children_5
	rename T01_19__6 TD_children_6
	rename T01_19__7 TD_children_7
	rename T01_19__8 TD_children_8
	rename T01_19__9 TD_children_9
	rename T01_20 TD_language
	rename T01_21 TD_transport_type
	rename T01_22 TD_transport_time
	rename T01_23 TD_transport_distance

	// Section 2

	rename T02_01 T_school_ocu
	rename T02_02 T_school_ocu_other
	rename T02_03 T_contract_def
	rename T02_04__1 TD_training_1
	rename T02_04__2 TD_training_2
	rename T02_04__3 TD_training_3
	rename T02_04__4 TD_training_4
	rename T02_04__5 TD_training_5
	rename T02_04__6 TD_training_6
	rename T02_05 TD_teach_this_school_years
	rename T02_06 TD_teach_other_school
	rename T02_07 TD_teach_other_school_years
	rename T02_08 TD_salary
	rename T02_09 TD_bonus_possible
	rename T02_10 TD_bonus_received
	rename T02_11__1 T_evaluation_how_1
	rename T02_11__2 T_evaluation_how_2
	rename T02_11__3 T_evaluation_how_3
	rename T02_11__4 T_evaluation_how_4
	rename T02_11__5 T_evaluation_how_5
	rename T02_11__6 T_evaluation_how_6
	rename T02_11__7 T_evaluation_how_7
	rename T02_11__8 T_evaluation_how_8
	rename T02_11__9 T_evaluation_how_9
	rename T02_11__10 T_evaluation_how_10
	rename T02_11__11 T_evaluation_how_11
	rename T02_12 TD_evaluation_who
	rename T02_13__1 TD_consequence_good_1
	rename T02_13__2 TD_consequence_good_2
	rename T02_13__3 TD_consequence_good_3
	rename T02_13__4 TD_consequence_good_4
	rename T02_13__5 TD_consequence_good_5
	rename T02_14__1 TD_consequence_bad_1
	rename T02_14__2 TD_consequence_bad_2
	rename T02_14__3 TD_consequence_bad_3
	rename T02_14__4 TD_consequence_bad_4
	rename T02_14__5 TD_consequence_bad_5
	rename T02_14__6 TD_consequence_bad_6
	rename T02_15 T_director_can_dismiss
	rename T02_16__1 TD_receive_help_1
	rename T02_16__2 TD_receive_help_2
	rename T02_16__3 TD_receive_help_3
	rename T02_16__4 TD_receive_help_4
	rename T02_17 TD_other_income
	rename T02_18 TD_other_income_type
	rename T02_19 TD_other_income_value

	// Section 3

	rename T03* TD03*

	// Section 4

	rename T04* TD05*

	// Sections 5 - 6

	rename T05* TD07*
	rename T06* TD08*

	// Section 7

	rename T07_01 TD_teacher_motivation
	rename T07_02 TD_teacher_responsibilities
	rename T07_03 TD_director_responsibilities
	rename T07_04 TD_parents_involvement
	rename T07_05 TD_parents_availablity
	rename T07_06 TD_school_good
	rename T07_07 T_perf_director
	rename T07_08 TD_perf_board
	rename T07_09 TD_perf_teachers
	rename T07_10 TD_perf_school
	rename T07_11 TD_perf_comm
	rename T07_12 TD_satis_salary
	rename T07_13 TD_satis_career

	// Sections 8 - 13

	rename T08* TD10*
	rename T09* TD11*
	rename T10* TD12*
	rename T11* TD13*
	rename T12* TD14*
	rename T13* TD15*

// Teachers survey ready to be appended


save "$Endline_dtRaw/_intermediate files/teachers_merge.dta", replace


// Do the same for the director survey


use "$Endline_dtRaw/_intermediate files/director.dta", clear


// Variables that need to be renamed

	// Section 1

	rename D01_01 TD_age
	rename D01_02 TD_female
	rename D01_03 TD_home_loc
	rename D01_03_other2 TD_home_other2
	rename D01_03_other3 TD_home_other3
	rename D01_04 TD_origin
	rename D01_04_other2 TD_origin_other2
	rename D01_04_other3 TD_origin_other3
	rename D01_05 TD_home_years
	rename D01_06 TD_edu
	rename D01_07 D_uni_years
	rename D01_08__1 TD_edu_edu
	rename D01_08__2 TD_edu_peda
	rename D01_08__3 TD_edu_soc
	rename D01_08__4 TD_edu_apl
	rename D01_08_other TD_edu_spec
	rename D01_10 D_ocu_dir
	rename D01_11 TD_ocu
	rename D01_12 TD_marital
	rename D01_13 TD_edu_partner
	rename D01_14 TD_ocu_partner
	rename D01_15 TD_home_type
	rename D01_16 TD_income
	rename D01_17 TD_children
	rename D01_18 TD_children_others
	rename D01_19 TD_children_girls
	rename D01_20 TD_children_school
	rename D01_21__0 TD_children_pre
	rename D01_21__1 TD_children_1
	rename D01_21__2 TD_children_2
	rename D01_21__3 TD_children_3
	rename D01_21__4 TD_children_4
	rename D01_21__5 TD_children_5
	rename D01_21__6 TD_children_6
	rename D01_21__7 TD_children_7
	rename D01_21__8 TD_children_8
	rename D01_21__9 TD_children_9
	rename D01_22 TD_language
	rename D01_23 TD_transport_type
	rename D01_24 TD_transport_time
	rename D01_25 TD_transport_distance

	// Section 2

	rename D02_01 D_schoolman_training
	rename D02_02__1 TD_training_1
	rename D02_02__2 TD_training_2
	rename D02_02__3 TD_training_3
	rename D02_02__4 TD_training_4
	rename D02_02__5 TD_training_5
	rename D02_02__6 TD_training_6
	rename D02_03 D_dir_other_school
	rename D02_04 D_dir_other_school_years
	rename D02_05 D_dir_this_school_years
	rename D02_06 D_taught_this_school
	rename D02_07 D_taught_this_school_years
	rename D02_08 TD_teach_this_school_years
	rename D02_09 TD_teach_other_school
	rename D02_10 TD_teach_other_school_years
	rename D02_11 TD_salary
	rename D02_12 TD_bonus_possible
	rename D02_13 TD_bonus_received
	rename D02_14__1 D_evaluation_how_1
	rename D02_14__2 D_evaluation_how_2
	rename D02_14__3 D_evaluation_how_3
	rename D02_14__4 D_evaluation_how_4
	rename D02_14__5 D_evaluation_how_5
	rename D02_14__6 D_evaluation_how_6
	rename D02_14__7 D_evaluation_how_7
	rename D02_14__8 D_evaluation_how_8
	rename D02_14__9 D_evaluation_how_9
	rename D02_14__10 D_evaluation_how_10
	rename D02_14__11 D_evaluation_how_11
	rename D02_14__12 D_evaluation_how_12
	rename D02_15 TD_evaluation_who
	rename D02_16__1 TD_consequence_good_1
	rename D02_16__2 TD_consequence_good_2
	rename D02_16__3 TD_consequence_good_3
	rename D02_16__4 TD_consequence_good_4
	rename D02_16__5 TD_consequence_good_5
	rename D02_17__1 TD_consequence_bad_1
	rename D02_17__2 TD_consequence_bad_2
	rename D02_17__3 TD_consequence_bad_3
	rename D02_17__4 TD_consequence_bad_4
	rename D02_17__5 TD_consequence_bad_5
	rename D02_17__6 TD_consequence_bad_6
	rename D02_18__1 TD_receive_help_1
	rename D02_18__2 TD_receive_help_2
	rename D02_18__3 TD_receive_help_3
	rename D02_19 TD_other_income
	rename D02_20 TD_other_income_type
	rename D02_21 TD_other_income_value

	// Section 3

	rename D03* TD03*

	// Section 4

	rename D04* DD04*

	// Section 5

	rename D05* TD05*

	// Section 6

	rename D06* DD06*

	// Sections 7 - 8

	rename D07* TD07*
	rename D08* TD08*

	// Section 9

	rename D09_01 TD_teacher_motivation
	rename D09_02 TD_teacher_responsibilities
	rename D09_03 TD_director_responsibilities
	rename D09_04 TD_parents_involvement
	rename D09_05 TD_parents_availablity
	rename D09_06 TD_school_good
	rename D09_07 TD_perf_board
	rename D09_08 TD_perf_teachers
	rename D09_09 TD_perf_school
	rename D09_10 TD_perf_comm
	rename D09_11 TD_satis_salary
	rename D09_12 TD_satis_career

	// Sections 10 - 15

	rename D10* TD10*
	rename D11* TD11*
	rename D12* TD12*
	rename D13* TD13*
	rename D14* TD14*
	rename D15* TD15*

// Director survey ready to be appended


// Appending the two surveys

	append using "$Endline_dtRaw/_intermediate files/teachers_merge.dta"


// Order misplaced variables from the teacher survey

	order director_survey, after(game)

	order T_ocu_teach, after(D_ocu_dir)

	order T_school_ocu T_school_ocu_other T_contract_def, 					///
		after(D_schoolman_training)

	order T_evaluation_how_1 T_evaluation_how_2 T_evaluation_how_3 			///
	T_evaluation_how_4 T_evaluation_how_5 T_evaluation_how_6 				///
	T_evaluation_how_7 T_evaluation_how_8 T_evaluation_how_9 				///
	T_evaluation_how_10 T_evaluation_how_11, after(D_evaluation_how_12)

	order T_director_can_dismiss, after(TD_consequence_bad_6)

	order TD_receive_help_4, after(TD_receive_help_3)

	order TD03_05_02__10, after(TD03_05_02__9)

	order TD03_05_03__10, after(TD03_05_03__9)

	order TD03_23, after(TD03_22)

	order TD03_24__10 TD03_24__11, after(TD03_24__9)

	order TD03_34, after(TD03_33)

	order TD05_05_02__13, after(TD05_05_02__12)

	order TD05_06_13, after(TD05_06_12)

	order TD05_07_13, after(TD05_07_12)

	order TD05_08_02__12 TD05_08_02__13, after(TD05_08_02__11)

	order TD05_09_12 TD05_10_12 TD05_09_13 TD05_10_13, after(TD05_10_11)

	order TD07_01__24 TD07_01__25 , after(TD07_01__23)

	order TD07_02__35, after(TD07_02__34)

	order TD07_36, after(TD07_35)

	order TD10_10__11, after(TD10_10__10)

	order TD12_03_4 TD12_04_4 TD12_05_4 TD12_06_4, after(TD12_06_3)

	order T_perf_director, after(TD_school_good)



	save "$Endline_dtRaw/_intermediate files/TD.dta", replace


// 	Teachers also have a roster in the school survey, let's add that :)

	// First need to get the school IDs from the school surveys

		use "$Endline_dtRaw/_intermediate files/school.dta", clear

		keep ss_key school_id

		order school_id ss_key

		merge 1:m ss_key using "$Endline_dtRaw/_intermediate files/school_S07.dta"

	// Create identification variables

		sort school_id

		drop roster_id_S07 ss_key _merge

		gen teacher_id = substr(S07_05_T,-2,.)

		gen director_id = substr(S07_05_D,-2,.)

		destring teacher_id director_id, replace

		sort school_id teacher_id

		order school_id teacher_id director_id

	// Correction made for uniqueness and solving misstakes

		replace teacher_id =15 if S07_02=="Maria Caldeira"
		replace teacher_id =16 if S07_02=="Arlete António"
		replace teacher_id =3 if S07_02=="Marcelina Nandita"
		replace teacher_id =14 if S07_02=="sonya figueira de oliveira"
		replace teacher_id =40 if S07_02=="emilia domingos camilo"
		replace teacher_id =11 if S07_02=="Victoria Catahenga"
		replace teacher_id =15 if S07_02=="Elisa Antonio"
		replace teacher_id =20 if S07_02=="Emilia Nalombe"

		drop if S07_02=="Ester Albino" & S07_09 == .

		replace teacher_id =20 if S07_02=="Esmeralda N. Lucamba"
		replace teacher_id =8 if S07_02=="Azevedo de Carvalho"
		replace teacher_id =12 if S07_02=="Elsa Raquel Domingo mario"
		replace teacher_id =21 if S07_02=="Teresa de Jesus dos Santos"
		replace teacher_id =20 if S07_02=="Júlia M.N. Salupupa"
		replace teacher_id =33 if S07_02=="Esperança J. dos Santos"
		replace teacher_id =37 if S07_02=="Sónia E. de C. Tavares"
		replace teacher_id =8 if S07_02=="Beataria Fonseca Domingos"
		replace teacher_id =15 if S07_02=="Amelia Araujp"
		replace teacher_id =33 if S07_02=="Beatriz   Ventura"
		replace teacher_id =13 if S07_02=="Mauricio Herculano Soma"
		replace teacher_id =12 if S07_02=="Teresa Pandico Adriano"

		replace teacher_id =13 if S07_02=="Mateus Pandieira"
		replace teacher_id =12 if S07_02=="Cecilia fonseca"
		replace teacher_id =13 if S07_02=="Carlos Gomes"
		replace teacher_id =12 if S07_02=="Celeste Manuel"
		replace teacher_id =24 if S07_02=="Izedine Fernando sateca"

	// Director IDs

		replace teacher_id = director_id if director_id>3 & director_id!=.

		replace director_id = . if director_id>3

		replace teacher_id = 3 if S07_02=="Mateus dos Santos"
		replace teacher_id = 2 if S07_02=="Lucas Benjamin Cambambi"
		replace teacher_id = 1 if S07_02=="Adelino culembala"
		replace teacher_id = 1 if S07_02=="Alberto Chiwale"
		replace teacher_id = 12 if S07_02=="Madalena Joaquim R. Franco"
		replace teacher_id = 10 if S07_02=="Domingos Silveira"
		replace teacher_id = 7 if S07_02=="David Armando António"
		replace teacher_id = 2 if S07_02=="Manurl  Bento  viola  Afonso"
		replace teacher_id = 2 if S07_02=="Constantino Gomes"
		replace teacher_id = 2 if S07_02=="António Januário"
		replace teacher_id = 1 if S07_02=="Domingos prazeres António"
		replace teacher_id = 2 if S07_02=="Domingos Papagaio"
		replace teacher_id = 6 if S07_02=="Joaquim Maneco"
		replace teacher_id = 12 if S07_02=="Mariquinha Patrocínio"
		replace teacher_id = 1 if S07_02=="Manuel Lopes Albino"
		replace teacher_id = 5 if S07_02=="Armando Januário Francisco"
		replace teacher_id = 2 if S07_02=="Adolfo Alfredo Ramos"
		replace teacher_id = 6 if S07_02=="Martins Furtado Manuel"
		replace teacher_id = 14 if S07_02=="Tomás Matias dos Santos"
		replace teacher_id = 11 if S07_02=="Moráis Canhanga"
		replace teacher_id = 8 if S07_02=="Miguel Pedro António da Silva"
		replace teacher_id = 5 if S07_02=="José Frederico Matias"
		replace teacher_id = 6 if S07_02=="Fernando Araújo"
		replace teacher_id = 1 if S07_02=="Laurindo Matias"

	// Correction after merging

		replace teacher_id = 1 if S07_02=="Marcelina Restino"
		replace teacher_id = 24 if S07_02=="Norberta Augusto"
		replace teacher_id = 25 if S07_02=="Domingas Lopes A. Adriano"
		replace teacher_id = 45 if S07_02=="Albertina  Arnaldo dos Santos"
		replace teacher_id = 14 if S07_02=="Jacinto Miguel Pessoa Bumba"

		replace teacher_id = 98 if S07_02=="Raúl Ernesto Mungongo"

		drop if teacher_id==.

	// Merging

		gen teacher_id2 = 100*school_id + teacher_id

		drop teacher_id director_id S07_05_T S07_05_D

		order teacher_id2

		sort teacher_id2

		replace teacher_id2=2388 if teacher_id2==2302
		replace teacher_id2=3688 if teacher_id2==3607
		replace teacher_id2=3888 if teacher_id2==3814
		replace teacher_id2=3988 if teacher_id2==3910
		replace teacher_id2=4988 if teacher_id2==4910
		replace teacher_id2=5888 if teacher_id2==5806
		replace teacher_id2=6688 if teacher_id2==6617
		replace teacher_id2=8288 if teacher_id2==8216
		replace teacher_id2=10688 if teacher_id2==10616
		replace teacher_id2=11588 if teacher_id2==11508
		replace teacher_id2=11688 if teacher_id2==11606


		merge 1:1 teacher_id2 using "$Endline_dtRaw/_intermediate files/TD.dta"

		rename _merge merge_Std_Rst


// Merge with the games data for teachers and directors


	merge 1:1 teacher_id2 using "$Endline_dtRaw/_intermediate files/games_teachers.dta"

// Director IDs need to be adjusted for matching with tests if teacher in 3,4,5.

	replace teacher_id2=2302 if teacher_id2==2388
	replace teacher_id2=2801 if teacher_id2==2888
	replace teacher_id2=3701 if teacher_id2==3788
	replace teacher_id2=4802 if teacher_id2==4888
	replace teacher_id2=5901 if teacher_id2==5988
	replace teacher_id2=6301 if teacher_id2==6388
	replace teacher_id2=6702 if teacher_id2==6788
	replace teacher_id2=7006 if teacher_id2==7088
	replace teacher_id2=7201 if teacher_id2==7288
	replace teacher_id2=10616 if teacher_id2==10688
	replace teacher_id2=3607 if teacher_id2==3688
	replace teacher_id2=3814 if teacher_id2==3888
	replace teacher_id2=3910 if teacher_id2==3988
	replace teacher_id2=4402 if teacher_id2==4488
	replace teacher_id2=4910 if teacher_id2==4988
	replace teacher_id2=5305 if teacher_id2==5388
	replace teacher_id2=5604 if teacher_id2==5688
	replace teacher_id2=5806 if teacher_id2==5888
	replace teacher_id2=6002 if teacher_id2==6088
	replace teacher_id2=6617 if teacher_id2==6688
	replace teacher_id2=8603 if teacher_id2==8688
	replace teacher_id2=8216 if teacher_id2==8288
	replace teacher_id2=10005 if teacher_id2==10088
	replace teacher_id2=11508 if teacher_id2==11588
	replace teacher_id2=11606 if teacher_id2==11688

	replace director_survey = 1 if ss_key=="33-55-96-76"
	replace director_survey = 1 if ss_key=="62-64-32-32"
	replace director_survey = 1 if ss_key=="12-28-91-84"

	local TD03 	TD03_01_01 TD03_01_01_01 TD03_01_02_01 TD03_01_03_01 TD03_02_01 TD03_03_01 TD03_04_01 TD03_05_01__1 TD03_05_01__2 TD03_05_01__3 TD03_05_01__4 TD03_05_01__5 TD03_05_01__6 TD03_05_01__7 TD03_05_01__8 TD03_05_01__9 TD03_05_01__10 TD03_05_01__11 TD03_06_01 TD03_07_01 TD03_08_01 TD03_09_01 TD03_10_01 TD03_01_01_02 TD03_01_02_02 TD03_01_03_02 TD03_02_02 TD03_03_02 TD03_04_02 TD03_05_02__1 TD03_05_02__2 TD03_05_02__3 TD03_05_02__4 TD03_05_02__5 TD03_05_02__6 TD03_05_02__7 TD03_05_02__8 TD03_05_02__9 TD03_05_02__10 TD03_06_02 TD03_07_02 TD03_08_02 TD03_09_02 TD03_10_02 TD03_01_01_03 TD03_01_02_03 TD03_01_03_03 TD03_02_03 TD03_03_03 TD03_04_03 TD03_05_03__1 TD03_05_03__2 TD03_05_03__3 TD03_05_03__4 TD03_05_03__5 TD03_05_03__6 TD03_05_03__7 TD03_05_03__8 TD03_05_03__9 TD03_05_03__10 TD03_06_03 TD03_07_03 TD03_08_03 TD03_09_03 TD03_10_03 TD03_11__0 TD03_11__1 TD03_11__2 TD03_11__3 TD03_11__4 TD03_11__5 TD03_11__6 TD03_11__7 TD03_11__8 TD03_11__9 TD03_11__11 TD03_11__22 TD03_11__33 TD03_11_00 TD03_11_01 TD03_11_02 TD03_11_03 TD03_11_04 TD03_11_05 TD03_11_06 TD03_11_07 TD03_11_08 TD03_11_09 TD03_13 TD03_14 TD03_15__1 TD03_15__2 TD03_15__3 TD03_15__4 TD03_15__5 TD03_15__6 TD03_15__7 TD03_15__8 TD03_16 TD03_17 TD03_18 TD03_19 TD03_20 TD03_21 TD03_22 TD03_23 TD03_24__1 TD03_24__2 TD03_24__3 TD03_24__4 TD03_24__5 TD03_24__6 TD03_24__7 TD03_24__8 TD03_24__9 TD03_24__10 TD03_24__11 TD03_25 TD03_26 TD03_27 TD03_28 TD03_29 TD03_30 TD03_31 TD03_32 TD03_33 TD03_34
	local TD15 	TD15_01 TD15_02 TD15_03_1 TD15_04_1 TD15_05_1 TD15_06_1 TD15_07_1 TD15_08_1 TD15_09_1 TD15_10_1 TD15_11_1 TD15_12_1 TD15_13_1 TD15_03_2 TD15_04_2 TD15_05_2 TD15_06_2 TD15_07_2 TD15_08_2 TD15_09_2 TD15_10_2 TD15_11_2 TD15_12_2 TD15_13_2 TD15_03_3 TD15_04_3 TD15_05_3 TD15_06_3 TD15_07_3 TD15_08_3 TD15_09_3 TD15_10_3 TD15_11_3 TD15_12_3 TD15_13_3 TD15_03_4 TD15_04_4 TD15_05_4 TD15_06_4 TD15_07_4 TD15_08_4 TD15_09_4 TD15_10_4 TD15_11_4 TD15_12_4 TD15_13_4 TD15_03_5 TD15_04_5 TD15_05_5 TD15_06_5 TD15_07_5 TD15_08_5 TD15_09_5 TD15_10_5 TD15_11_5 TD15_12_5 TD15_13_5 TD15_03_6 TD15_04_6 TD15_05_6 TD15_06_6 TD15_07_6 TD15_08_6 TD15_09_6 TD15_10_6 TD15_11_6 TD15_12_6 TD15_13_6 TD15_03_7 TD15_04_7 TD15_05_7 TD15_06_7 TD15_07_7 TD15_08_7 TD15_09_7 TD15_10_7 TD15_11_7 TD15_12_7 TD15_13_7 TD15_03_8 TD15_04_8 TD15_05_8 TD15_06_8 TD15_07_8 TD15_08_8 TD15_09_8 TD15_10_8 TD15_11_8 TD15_12_8 TD15_13_8 TD15_03_9 TD15_04_9 TD15_05_9 TD15_06_9 TD15_07_9 TD15_08_9 TD15_09_9 TD15_10_9 TD15_11_9 TD15_12_9 TD15_13_9 TD15_03_10 TD15_04_10 TD15_05_10 TD15_06_10 TD15_07_10 TD15_08_10 TD15_09_10 TD15_10_10 TD15_11_10 TD15_12_10 TD15_13_10 TD15_03_11 TD15_04_11 TD15_05_11 TD15_06_11 TD15_07_11 TD15_08_11 TD15_09_11 TD15_10_11 TD15_11_11 TD15_12_11 TD15_13_11 TD15_03_12 TD15_04_12 TD15_05_12 TD15_06_12 TD15_07_12 TD15_08_12 TD15_09_12 TD15_10_12 TD15_11_12 TD15_12_12 TD15_13_12 TD15_03_13 TD15_04_13 TD15_05_13 TD15_06_13 TD15_07_13 TD15_08_13 TD15_09_13 TD15_10_13 TD15_11_13 TD15_12_13 TD15_13_13 TD15_03_14 TD15_04_14 TD15_05_14 TD15_06_14 TD15_07_14 TD15_08_14 TD15_09_14 TD15_10_14 TD15_11_14 TD15_12_14 TD15_13_14 TD15_03_15 TD15_04_15 TD15_05_15 TD15_06_15 TD15_07_15 TD15_08_15 TD15_09_15 TD15_10_15 TD15_11_15 TD15_12_15 TD15_13_15 TD15_03_16 TD15_04_16 TD15_05_16 TD15_06_16 TD15_07_16 TD15_08_16 TD15_09_16 TD15_10_16 TD15_11_16 TD15_12_16 TD15_13_16 TD15_03_17 TD15_04_17 TD15_05_17 TD15_06_17 TD15_07_17 TD15_08_17 TD15_09_17 TD15_10_17 TD15_11_17 TD15_12_17 TD15_13_17 TD15_03_18 TD15_04_18 TD15_05_18 TD15_06_18 TD15_07_18 TD15_08_18 TD15_09_18 TD15_10_18 TD15_11_18 TD15_12_18 TD15_13_18 TD15_03_19 TD15_04_19 TD15_05_19 TD15_06_19 TD15_07_19 TD15_08_19 TD15_09_19 TD15_10_19 TD15_11_19 TD15_12_19 TD15_13_19 TD15_03_20 TD15_04_20 TD15_05_20 TD15_06_20 TD15_07_20 TD15_08_20 TD15_09_20 TD15_10_20 TD15_11_20 TD15_12_20 TD15_13_20
	local teach resp_teach resp_grade__0 resp_grade__1 resp_grade__2 resp_grade__3 resp_grade__4 resp_grade__5 resp_grade__6 resp_grade__7 resp_grade__8 resp_grade__9 resp_grade__11 resp_grade__22 resp_grade__33

	sort teacher_id2 director_survey

	foreach var in `TD03' `TD15'{
	replace `var' = `var'[_n-1] if inlist(teacher_id2,4402,6702,7201,8603) & survey_type==3
	 }

	foreach var in `teach'{
	replace `var' = `var'[_n-1] if inlist(teacher_id2,8603) & survey_type==3
	 }

	drop if director_survey==1

// Rename introduction variables so that they do not conflict with parents survey

	foreach var in ss_key ss_id ss_random ss_errors survey_type game 		///
	director_survey school_id resp_id municipality treatment supervisor 	///
	interviewer date_time_start date_time_end resp_name resp_tel resp_pos 	///
	resp_teach resp_grade__0 resp_grade__1 resp_grade__2 resp_grade__3 		///
	resp_grade__4 resp_grade__5 resp_grade__6 resp_grade__7 resp_grade__8 	///
	resp_grade__9 resp_grade__11 resp_grade__22 resp_grade__33 phone{
	rename `var' TD_`var'
	}

	rename merge_network merge_Gdt_Ndt
	rename _merge merge_Sdt_GNdt

	rename n_* Ndt_*

	foreach var in teacher_id3 school_name blue_id teacher_name teacher_id 	///
	date month leader1 leader2_1 leader2_2 blue orange version dg 			///
	dg_belief0 dg_belief500 dg_belief1000 dg_belief1500 dg_belief2000 		///
	tg_return200 tg_return400 tg_return600 tg_return800 pgg pgg_belief 		///
	pgg_belief_old {
	rename `var' Gdt_`var'
	}

	/* School 11 had a wrong FdA file at the first visits, therefore we cannot
		use section 15 of the the teacher and director surveys of this school
	*/

	replace TD15_01 = . if TD_school_id==11
	replace TD15_02 = . if TD_school_id==11

	forval x = 1/20{
	replace TD15_03_`x' = .		if TD_school_id==11
	replace TD15_04_`x' =  ""	if TD_school_id==11
	replace TD15_05_`x' = .		if TD_school_id==11
	replace TD15_06_`x' = . 	if TD_school_id==11
	replace TD15_07_`x' =  ""	if TD_school_id==11
	replace TD15_08_`x' = . 	if TD_school_id==11
	replace TD15_09_`x' = . 	if TD_school_id==11
	replace TD15_10_`x' = . 	if TD_school_id==11
	replace TD15_11_`x' = . 	if TD_school_id==11
	replace TD15_12_`x' = . 	if TD_school_id==11
	replace TD15_13_`x' = .		if TD_school_id==11
	}

	// Four "teachers" are not really teachers but admin staff, here is a indicator

	gen TD_staff_member = 0
	replace TD_staff_member = 1 if inlist(teacher_id2,977,2977,3577,12477)
	order TD_staff_member, after(TD_resp_pos)


// Teacher and director file is ready for merging with other datasets

save "$Endline_dtRaw/_intermediate files/TDG.dta", replace


// Link the parents survey with the games data for parents

	// First get the roster from teachers and director about parents

	keep teacher_id2 TD_school_id T_school_ocu TD_resp_name T_contract_def TD03_01_01_01 ///
	TD03_01_02_01 TD03_01_01_02 TD03_01_02_02 TD03_01_01_03 TD03_01_02_03 	///
	TD15_01 TD15_02 TD15_03_1 TD15_04_1 TD15_05_1 TD15_06_1 TD15_07_1 		///
	TD15_08_1 TD15_09_1 TD15_10_1 TD15_11_1 TD15_12_1 TD15_13_1 TD15_03_2 	///
	TD15_04_2 TD15_05_2 TD15_06_2 TD15_07_2 TD15_08_2 TD15_09_2 TD15_10_2 	///
	TD15_11_2 TD15_12_2 TD15_13_2 TD15_03_3 TD15_04_3 TD15_05_3 TD15_06_3 	///
	TD15_07_3 TD15_08_3 TD15_09_3 TD15_10_3 TD15_11_3 TD15_12_3 TD15_13_3 	///
	TD15_03_4 TD15_04_4 TD15_05_4 TD15_06_4 TD15_07_4 TD15_08_4 TD15_09_4 	///
	TD15_10_4 TD15_11_4 TD15_12_4 TD15_13_4 TD15_03_5 TD15_04_5 TD15_05_5 	///
	TD15_06_5 TD15_07_5 TD15_08_5 TD15_09_5 TD15_10_5 TD15_11_5 TD15_12_5 	///
	TD15_13_5 TD15_03_6 TD15_04_6 TD15_05_6 TD15_06_6 TD15_07_6 TD15_08_6 	///
	TD15_09_6 TD15_10_6 TD15_11_6 TD15_12_6 TD15_13_6 TD15_03_7 TD15_04_7 	///
	TD15_05_7 TD15_06_7 TD15_07_7 TD15_08_7 TD15_09_7 TD15_10_7 TD15_11_7 	///
	TD15_12_7 TD15_13_7 TD15_03_8 TD15_04_8 TD15_05_8 TD15_06_8 TD15_07_8 	///
	TD15_08_8 TD15_09_8 TD15_10_8 TD15_11_8 TD15_12_8 TD15_13_8 TD15_03_9 	///
	TD15_04_9 TD15_05_9 TD15_06_9 TD15_07_9 TD15_08_9 TD15_09_9 TD15_10_9 	///
	TD15_11_9 TD15_12_9 TD15_13_9 TD15_03_10 TD15_04_10 TD15_05_10 			///
	TD15_06_10 TD15_07_10 TD15_08_10 TD15_09_10 TD15_10_10 TD15_11_10 		///
	TD15_12_10 TD15_13_10 TD15_03_11 TD15_04_11 TD15_05_11 TD15_06_11 		///
	TD15_07_11 TD15_08_11 TD15_09_11 TD15_10_11 TD15_11_11 TD15_12_11 		///
	TD15_13_11 TD15_03_12 TD15_04_12 TD15_05_12 TD15_06_12 TD15_07_12 		///
	TD15_08_12 TD15_09_12 TD15_10_12 TD15_11_12 TD15_12_12 TD15_13_12 		///
	TD15_03_13 TD15_04_13 TD15_05_13 TD15_06_13 TD15_07_13 TD15_08_13 		///
	TD15_09_13 TD15_10_13 TD15_11_13 TD15_12_13 TD15_13_13 TD15_03_14 		///
	TD15_04_14 TD15_05_14 TD15_06_14 TD15_07_14 TD15_08_14 TD15_09_14 		///
	TD15_10_14 TD15_11_14 TD15_12_14 TD15_13_14 TD15_03_15 TD15_04_15 		///
	TD15_05_15 TD15_06_15 TD15_07_15 TD15_08_15 TD15_09_15 TD15_10_15 		///
	TD15_11_15 TD15_12_15 TD15_13_15 TD15_03_16 TD15_04_16 TD15_05_16 		///
	TD15_06_16 TD15_07_16 TD15_08_16 TD15_09_16 TD15_10_16 TD15_11_16 		///
	TD15_12_16 TD15_13_16 TD15_03_17 TD15_04_17 TD15_05_17 TD15_06_17 		///
	TD15_07_17 TD15_08_17 TD15_09_17 TD15_10_17 TD15_11_17 TD15_12_17 		///
	TD15_13_17 TD15_03_18 TD15_04_18 TD15_05_18 TD15_06_18 TD15_07_18 		///
	TD15_08_18 TD15_09_18 TD15_10_18 TD15_11_18 TD15_12_18 TD15_13_18 		///
	TD15_03_19 TD15_04_19 TD15_05_19 TD15_06_19 TD15_07_19 TD15_08_19 		///
	TD15_09_19 TD15_10_19 TD15_11_19 TD15_12_19 TD15_13_19 TD15_03_20 		///
	TD15_04_20 TD15_05_20 TD15_06_20 TD15_07_20 TD15_08_20 TD15_09_20 		///
	TD15_10_20 TD15_11_20 TD15_12_20 TD15_13_20

	reshape long 	TD15_03_ TD15_04_ TD15_05_ TD15_06_ TD15_07_ TD15_08_ 	///
					TD15_09_ TD15_10_ TD15_11_ TD15_12_ TD15_13_			///
			, i(teacher_id2) j(n)

	rename *_ *

	drop n

	drop if inlist(TD15_03,.)

	gen parent_id2 = 100*TD_school_id + TD15_03

	order parent_id2 TD15_04, after(teacher_id2)

	sort parent_id2 teacher_id2

	// Correcting so that each student has only one teacher

	drop if teacher_id2==342 & parent_id2==315
	replace parent_id2 = 617 if TD15_04=="Siumara Culembe"
	replace parent_id2 = 628 if TD15_04=="Arieth Armando"
	drop if teacher_id2==720 & parent_id2==716
	drop if teacher_id2==720 & parent_id2==717
	drop if teacher_id2==720 & parent_id2==718
	replace parent_id2 =1618  if TD15_04=="Teresa Adolfo"
	replace parent_id2 = 1818 if TD15_04=="Osvaldo G. A. Lenge"
	replace parent_id2 = 2225  if TD15_04=="Alberta Domingos Fernando Januário"
	replace parent_id2 = 2317 if TD15_04=="Ageu Maurício de Graça"
	replace parent_id2 = 3013 if TD15_04=="Maria Feliciano Pedro"
	replace parent_id2 = 3513 if TD15_04=="Lidiane Branco"
	replace parent_id2 = 3529 if TD15_04=="Josefina Samuel"
	drop if TD15_04=="Samuel Francisco P.Agostinho"
	replace parent_id2 = 4330  if TD15_04=="Beatriz Paulo do Espírito Santo"
	replace parent_id2 = 5834 if TD15_04=="jose André kifanda"
	replace parent_id2 = 6332 if TD15_04=="Amilson jose ferramenta"
	replace parent_id2 = 6333 if TD15_04=="Antonios paulino"
	replace parent_id2 = 7629 if TD15_04=="Angilberto  Dúlio N. Gonsalves"
	replace parent_id2 = 8020 if TD15_04=="Florinda Silvestre João"
	drop if TD15_04=="José Domingos E. Pinto"
	replace parent_id2 = 8330 if TD15_04=="Ponoy cesar domingos americano"
	replace parent_id2 = 8403 if TD15_04=="Cecília Tumba Manuel Valentim"
	replace parent_id2 = 8505 if TD15_04=="José Luiz"
	replace parent_id2 = 9006 if TD15_04=="Marcelo Antonio da Silva"
	drop if TD15_04=="Luísa Zélia Joaquim Fansony"
	replace parent_id2 = 9305 if TD15_04=="António Jeremias"
	replace parent_id2 = 9329 if TD15_04=="Ramiro Damião"
	replace parent_id2 = 10306 if TD15_04=="Augusto Alfredo Adriano"
	drop if teacher_id2==11209 & parent_id2==11208
	drop if teacher_id2==11209 & parent_id2==11209
	replace parent_id2 = 11225  if TD15_04=="Alfredo António Manuel"
	replace parent_id2 = 11316 if TD15_04=="Regina Lucas Albino"
	drop if teacher_id2==11402 & parent_id2==11405
	drop if teacher_id2==11402 & parent_id2==11407
	drop if teacher_id2==11402 & parent_id2==11408
	drop if teacher_id2==11403 & parent_id2==11409
	drop if teacher_id2==11403 & parent_id2==11410
	drop if teacher_id2==11403 & parent_id2==11411
	drop if teacher_id2==11403 & parent_id2==11412
	drop if teacher_id2==11406 & parent_id2==11421
	drop if teacher_id2==11406 & parent_id2==11430
	drop if teacher_id2==11408 & parent_id2==11432
	drop if teacher_id2==11409 & parent_id2==11437
	drop if teacher_id2==11409 & parent_id2==11439
	replace parent_id2 = 11607  if TD15_04=="Januário Francisco Santareno"
	replace parent_id2 = 11618 if TD15_04=="Cecília Faustudo"
	replace parent_id2 = 12008 if TD15_04=="Genito Chipinda"
	drop if teacher_id2==6702 & parent_id2==6701
	drop if teacher_id2==6702 & parent_id2==6702
	drop if teacher_id2==6702 & parent_id2==6703
	drop if teacher_id2==6702 & parent_id2==6704
	drop if teacher_id2==6702 & parent_id2==6705
	drop if teacher_id2==6702 & parent_id2==6706
	replace TD15_04 = "Maria Antonio" if parent_id2==2219
	replace parent_id2 = 6335 if TD15_04=="Eusebio Raúl António"
	replace parent_id2 = 1235 if TD15_04=="Maria Augusto C. Manuel"
	drop if teacher_id2==9903 & parent_id2==9913
	drop if teacher_id2==11406 & parent_id2==11422

	drop 	T_school_ocu T_contract_def TD03_01_01_01 			///
			TD03_01_02_01 TD03_01_01_02 TD03_01_02_02 TD03_01_01_03 		///
			TD03_01_02_03

	rename TD_school_id school_id

	order TD15_04, after(TD15_03)

	order parent_id2

	drop if parent_id2 == .

	save "$Endline_dtRaw/_intermediate files/parents_info_from_teachers.dta", replace


// Link the parents survey with the games data for parents

	use "$Endline_dtRaw/_intermediate files/parents.dta", clear

	merge 1:1 parent_id2 using "$Endline_dtRaw/_intermediate files/games_parents.dta"

	rename merge_network merge_Gp_Np
	rename _merge merge_Sp_GNp

	merge 1:1 parent_id2 using "$Endline_dtRaw/_intermediate files/parents_info_from_teachers.dta"

	rename _merge merge_SGNp_Rpt

	sort parent_id2

	order parent_id2 teacher_id2

	drop if resp_id==41

// Rename variables to allow for matching

	foreach var in ss_key ss_id ss_random ss_errors survey_type selection 	///
	resp_id supervisor interviewer  	///
	date_time_start date_time_end resp_name resp_tel student_name nome  	///
	student_grade resp_relation location arrivedalone{
	rename `var' P_`var'
	}


	foreach var in parent_id3 school_name orange_id parent_name parent_id  	///
	date month leader1 leader2_1 leader2_2 blue orange version dg_faces0  	///
	dg_faces500 dg_faces1000 dg_faces1500 dg_faces2000 dg_belief0  	///
	dg_belief500 dg_belief1000 dg_belief1500 dg_belief2000 tg pgg  	///
	pgg_belief pgg_belief_old mismatch phone {
	rename `var' Gp_`var'
	}

	rename n_* Np_*
	rename TD* P_TD*
	order Gp* Np* O* P_TD* merge*, after(P11_03_20)

	rename teacher_id2 P_teacher_id2

	save "$Endline_dtRaw/_intermediate files/PG.dta", replace


// Start with tests

use "$Endline_dtRaw/_intermediate files/tests.dta", clear

// Keep what is necessary

keep 	school_id test_id test_type grade class student_id 	///
		shift teacher_id enumerator_id parent_id score nonfill

drop if school_id==999


	replace teacher_id = 2 if school_id==5 & grade==3 & class==3
	replace teacher_id = 19 if school_id==10 & grade==5 & class==7
	replace teacher_id = 4 if school_id==37 & grade==5 & class==1
	replace teacher_id = 3 if school_id==69 & grade==3 & class==1
	replace teacher_id = 7 if school_id==69 & grade==5 & class==2
	replace teacher_id = 8 if school_id==69 & grade==5 & class==3
	replace teacher_id = 1 if school_id==73 & grade==3 & class==1
	replace teacher_id = 2 if school_id==73 & grade==3 & class==2
	replace teacher_id = 8 if school_id==75 & grade==3 & class==1
	replace teacher_id = 2 if school_id==82 & grade==3 & class==1
	replace teacher_id = 3 if school_id==82 & grade==3 & class==2
	replace teacher_id = 18 if school_id==91 & grade==4 & class==9
	replace teacher_id = 3 if school_id==95 & grade==4 & class==1
	replace teacher_id = 1 if school_id==102 & grade==3 & class==1
	replace teacher_id = 24 if school_id==109 & grade==3 & class==2
	replace teacher_id = 3 if school_id==112 & grade==3 & class==3
	replace teacher_id = 7 if school_id==126 & grade==5 & class==1
	replace teacher_id = 1 if school_id==82 & grade==3 & class==3
	replace teacher_id = 4 if school_id==96 & grade==4 & class==2
	replace teacher_id = 3 if school_id==35 & grade==3 & class==3
	replace teacher_id = 14 if school_id==50 & grade==3 & class==1

	replace teacher_id = 8 if school_id==14 & grade==5 & class==1
	replace teacher_id = 22 if school_id==15 & grade==4 & class==5
	replace teacher_id = 4 if school_id==83 & grade==3 & class==4

	replace teacher_id = 11 if school_id==91 & parent_id==22 & test_type==2
	replace class = 3 if school_id==91 & parent_id==22 & test_type==2
	replace student_id = 46 if school_id==91 & parent_id==22 & test_type==2

	drop if school_id==91 & parent_id==19 & teacher_id==10



sort school_id grade class shift student_id

// Some corrections found through matching

replace teacher_id = 2 if school_id==2 & teacher_id==35
replace teacher_id = 7 if school_id==3 & teacher_id==5
replace teacher_id = 4 if school_id==13 & grade==4 & class==2
replace teacher_id = 8 if school_id==14 & teacher_id==3
replace teacher_id = 22 if school_id==15 & teacher_id==7
replace teacher_id = 14 if school_id==50 & teacher_id==1
replace teacher_id = 7 if school_id==119 & grade==3 & class==8

replace parent_id = 7 if school_id==10 & grade==3 & class==6 & student_id==1
replace parent_id = 8 if school_id==10 & grade==3 & class==3 & student_id==1
replace parent_id = . if school_id==10 & grade==4 & class==4 & student_id==1
replace parent_id = . if school_id==11 & grade==3 & class==1 & student_id==33
replace parent_id = 35 if school_id==12 & grade==5 & class==4 & student_id==1
replace parent_id = 36 if school_id==12 & grade==5 & class==4 & student_id==2
replace class = 3 if school_id==21 & class==20
replace parent_id = 6 if school_id==21 & grade==3 & class==3 & student_id==2
replace parent_id = . if school_id==24 & grade==5 & class==1 & student_id==19
replace parent_id = . if school_id==28 & grade==4 & class==3 & student_id==26
replace parent_id = 35 if school_id==35 & grade==4 & class==1 & student_id==3
replace parent_id = 11 if school_id==40 & grade==3 & class==1 & student_id==1
replace parent_id = . if school_id==43 & grade==3 & class==7 & student_id==26
replace school_id = 45 if school_id==54 & grade==5
replace parent_id = 39 if school_id==67 & grade==5 & class==2 & student_id==3
replace parent_id = 32 if school_id==70 & grade==3 & class==1 & student_id==1
replace parent_id = . if school_id==71 & grade==5 & class==3 & student_id==37
replace parent_id = . if school_id==76 & grade==5 & class==1 & student_id==3
replace parent_id = . if school_id==81 & grade==4 & class==5 & student_id==34
replace parent_id = 18 if school_id==84 & grade==4 & class==1 & student_id==10
replace parent_id = 9 if school_id==87 & grade==3 & class==5 & student_id==1
drop if school_id==87 & grade==5 & class==8 & student_id==1
drop if school_id==87 & grade==5 & class==8 & student_id==2
replace parent_id = . if school_id==91 & grade==4 & class==4 & student_id==1
replace parent_id = . if school_id==94 & grade==3 & class==2 & student_id==13
replace parent_id = . if school_id==97 & grade==3 & class==1 & student_id==32
replace parent_id = 19 if school_id==102 & grade==4 & class==4 & student_id==4
drop if school_id==119 & grade==3 & class==7
replace parent_id = . if school_id==121 & grade==3 & class==1 & student_id==19
replace parent_id = . if school_id==83 & grade==4 & class==3 & student_id==4
replace parent_id = . if school_id==96 & grade==4 & class==1 & student_id==16
replace parent_id = . if school_id==116 & grade==4 & class==1 & student_id==1
replace parent_id = 16 if school_id==118 & grade==4 & class==2 & student_id==3
replace parent_id = . if school_id==73 & grade==3 & class==2 & student_id==5
replace parent_id = . if school_id==35 & grade==5 & class==2 & student_id==1






replace teacher_id = 4 if school_id==13 & grade==4 & class==2

replace parent_id = . if school_id==7 & grade==3 & class==1 & student_id==5
replace parent_id = 7 if school_id==10 & grade==3 & class==6 & student_id==1
replace parent_id = 12 if school_id==11 & grade==3 & class==2 & student_id==4
replace parent_id = 35 if school_id==12 & grade==5 & class==4 & student_id==1
replace parent_id = 36 if school_id==12 & grade==5 & class==4 & student_id==2
replace parent_id = 12 if school_id==14 & grade==3 & class==1 & student_id==1
replace parent_id = . if school_id==15 & grade==4 & class==5 & student_id==32
replace parent_id = 25 if school_id==16 & grade==5 & class==3 & student_id==1
replace parent_id = . if school_id==20 & grade==3 & class==1 & student_id==22
replace parent_id = . if school_id==22 & grade==5 & class==2 & student_id==37
replace parent_id = . if school_id==24 & grade==4 & class==1 & student_id==6
drop if school_id==25 & grade==5
replace parent_id = 8 if school_id==28 & grade==3 & class==3 & student_id==7
replace parent_id = . if school_id==42 & grade==3 & class==2 & student_id==17
replace parent_id = 39 if school_id==50 & grade==5 & class==2 & student_id==5
replace parent_id = . if school_id==66 & grade==4 & class==2 & student_id==6
replace parent_id = . if school_id==76 & grade==5 & class==1 & student_id==3
replace parent_id = . if school_id==83 & grade==4 & class==3 & student_id==4
replace parent_id = 18 if school_id==84 & grade==4 & class==1 & student_id==10
replace parent_id = 18 if school_id==96 & grade==4 & class==1 & student_id==1
replace parent_id = . if school_id==99 & grade==5 & class==1 & student_id==19
replace parent_id = 3 if school_id==106 & grade==3 & class==2 & student_id==1
replace parent_id = . if school_id==116 & grade==4 & class==1 & student_id==1
replace parent_id = . if school_id==118 & grade==4 & class==2 & student_id==5
replace parent_id = . if school_id==118 & grade==4 & class==2 & student_id==8
replace parent_id = . if school_id==19 & grade==3 & class==6 & student_id==19
replace parent_id = 23 if school_id==21 & grade==4 & class==5 & student_id==1
replace parent_id = . if school_id==35 & grade==5 & class==2 & student_id==1
replace parent_id = . if school_id==68 & grade==4 & class==1 & student_id==4
replace parent_id = . if school_id==102 & grade==4 & class==4 & student_id==35







// Create matching IDs

gen parent_id2 = 100*school_id + parent_id
gen teacher_id2 = 100*school_id + teacher_id

lab var parent_id2 "Matching parent (survey) ID"
lab var teacher_id2 "Matching teacher (survey) ID"

order school_id teacher_id2 parent_id2

// Merge with parent survey

	sort school_id grade class student_id

	merge m:1 parent_id2 using "$Endline_dtRaw/_intermediate files/PG.dta"

	rename _merge merge_parents

	replace teacher_id2 = P_teacher_id2 if teacher_id2==.

	gen wrong_parent_id2 = 1 if teacher_id2!=P_teacher_id2
	replace wrong_parent_id2 = . if P_teacher_id2==.

	// Correcting for differences in teacher_id2


	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==7702
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==8201
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==9114
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==10607
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==11402
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==11908

	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==9111
	replace teacher_id2 = P_teacher_id2 if P_teacher_id2==11202

	drop wrong_parent_id2

	drop P_teacher_id2

	// Merge with teacher survey

	merge m:1 teacher_id2 using "$Endline_dtRaw/_intermediate files/TDG.dta"

	rename _merge merge_teachers


	/* 	Create a variable that excludes the director that do not teach
		These are directors out of which some played the games, but do not teach
		and therefore are not relevant at that place for the general analysis.
		Only for games
	*/

	gen exclude_nonteachers = 0
	replace exclude_nonteachers = 1 if TD_resp_teach==0


	// Rename the test variables

	lab var test_type "Portuguese (1) or Mathematics (2) test"

	rename grade 			test_grade
	rename class 			test_class
	rename shift  			test_period
	rename enumerator_id  	test_enumerator_id
	rename score 			test_score
	rename nonfill			test_nonfill

	drop teacher_id parent_id

	// Create new teacher and parent ID variables

	forval x = 1/126{
	replace school_id = `x' if teacher_id2>`x'00 & teacher_id2<=`x'99 & school_id==.
	}
	gen teacher_id = teacher_id2 - (school_id*100)
	replace teacher_id = 99 if teacher_id==98

	gen parent_id = parent_id2 - (school_id*100)

	order school_id teacher_id parent_id municipality treatment exclude_nonteachers

	drop teacher_id2 parent_id2

	// Municipality and treatment variables need to be renewed

	// Municipality

	replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,	///
	14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,		///
	40,45,61,86,110)

	replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,	///
	52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,	///
	77,78,79,80,81)

	replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,	///
	85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)

	replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
	replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
	replace municipality = 6 if inlist(school_id,109,111)
	replace municipality = 7 if inlist(school_id,106,107)
	replace municipality = 8 if inlist(school_id,126)
	replace municipality = 9 if inlist(school_id,108)

	// Treatment

	replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33, ///
	39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)

	replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51, ///
	56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)

	replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41, ///
	48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)

	replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43, ///
	44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

	// NETWORK SURVEYS

	rename O* Np*
	rename Gdt* Gtd*
	rename Ndt* Ntd*
	rename B* Ntd*
	rename TD_phone Ntd_phone

	// Variable labels

	lab var teacher_id 			"ID teacher"
	lab var parent_id 			"ID parent"
	lab var exclude_nonteachers	"Exclude for general analysis, include for games"


	// Organize the parents info from teachers

	order 	P_TD_resp_name P_TD15_01 P_TD15_02 P_TD15_03 P_TD15_04 			///
			P_TD15_05 P_TD15_06 P_TD15_07 P_TD15_08 P_TD15_09 P_TD15_10 	///
			P_TD15_11 P_TD15_12 P_TD15_13, after(P11_03_20)

	drop P_TD15_01 P_TD15_02 P_TD15_03

	label variable 		P_TD_resp_name	"Name teacher of student"
	label variable 		P_TD15_04 `"Student name (accoring to teacher)"'
	label variable 		P_TD15_05 `"This student is repeating the grade"'
	label variable 		P_TD15_06 `"This student has same teacher as last year"'
	label variable 		P_TD15_07 "Name of last year's teacher of student"
	label variable 		P_TD15_08 "ID of last year's teacher of student"
	label variable 		P_TD15_09 `"Teacher sent information in the last four weeks to this parent"'
	label variable 		P_TD15_10 `"Number of times this parent was invited for a meeting with the teacher (4weeks)"'
	label variable 		P_TD15_11 `"Number of times this parent came to school to talk to teacher (4weeks)"'
	label variable 		P_TD15_12 `"This parent and school share same objective"'
	label variable 		P_TD15_13 "This parent is involvement in student's school life"

	rename P_TD_resp_name 	Ptd_teacher_name
	rename P_TD15_04  		Ptd_student_name
	rename P_TD15_05  		Ptd_repeating
	rename P_TD15_06  		Ptd_teacher_same
	rename P_TD15_07  		Ptd_teacher_name_lastyear
	rename P_TD15_08  		Ptd_teacher_id_lastyear
	rename P_TD15_09  		Ptd_information
	rename P_TD15_10  		Ptd_invite
	rename P_TD15_11  		Ptd_meetings
	rename P_TD15_12  		Ptd_objective
	rename P_TD15_13		Ptd_involvement


	// Organize the teacher info from schools

	/* 	Basically what I need to do here is replace the missing info from the
		surveys. And add an extra variables for edu, salary, absentism.
		Other variables can be checked for consistency.

	*/

	order 	S07_02 S07_03 S07_04 S07_06 S07_07__0 S07_07__1 S07_07__2		///
			S07_07__3 S07_07__4 S07_07__5 S07_07__6 S07_07__7 S07_07__8 	///
			S07_07__9 S07_07__11 S07_07__12 S07_07__13 S07_08__0 S07_08__1 	///
			S07_08__2 S07_08__3 S07_08__4 S07_08__5 S07_08__6 S07_08__7 	///
			S07_08__8 S07_08__9 S07_08__11 S07_08__12 S07_08__13 S07_09 	///
			S07_10 S07_11 S07_12, after(TD15_13_20)

	rename S* TDs*

	// Create general gender indicator for teachers

	replace TD_female = TDs07_03 if school_id==6 & teacher_id==1
	replace TD_female = TDs07_03 if school_id==7 & teacher_id==10
	replace TD_female = TDs07_03 if school_id==21 & teacher_id==9
	replace TD_female = TDs07_03 if school_id==93 & teacher_id==2
	replace TD_female = TDs07_03 if school_id==101 & teacher_id==3
	replace TD_female = TDs07_03 if school_id==112 & teacher_id==3
	replace TD_female = TDs07_03 if school_id==114 & teacher_id==1
	replace TD_female = TDs07_03 if school_id==21 & teacher_id==39
	replace TD_female = TDs07_03 if school_id==39 & teacher_id==6
	replace TD_female = TDs07_03 if school_id==39 & teacher_id==7
	replace TD_female = TDs07_03 if school_id==95 & teacher_id==11
	replace TD_female = TDs07_03 if school_id==101 & teacher_id==11
	replace TD_female = TDs07_03 if school_id==109 & teacher_id==33
	replace TD_female = TDs07_03 if school_id==126 & teacher_id==11

	replace TD_female = TDs07_03 if TD_female==.
	replace TDs07_03 = TD_female if TDs07_03!=TD_female

	drop TDs07_02

	// Years of teaching

	order TDs07_06, after(TD_teach_this_school_years)

	rename TDs07_06 TDs_teach_this_school_years
	lab var TDs_teach_this_school_years "(SCHOOL) Years teaching at this school"

	// Grade of teaching

	replace TD_resp_grade__0 = TDs07_07__0 if TD_resp_grade__0==.
	replace TD_resp_grade__1 = TDs07_07__1 if TD_resp_grade__1==.
	replace TD_resp_grade__2 = TDs07_07__2 if TD_resp_grade__2==.
	replace TD_resp_grade__3 = TDs07_07__3 if TD_resp_grade__3==.
	replace TD_resp_grade__4 = TDs07_07__4 if TD_resp_grade__4==.
	replace TD_resp_grade__5 = TDs07_07__5 if TD_resp_grade__5==.
	replace TD_resp_grade__6 = TDs07_07__6 if TD_resp_grade__6==.
	replace TD_resp_grade__7 = TDs07_07__7 if TD_resp_grade__7==.
	replace TD_resp_grade__8 = TDs07_07__8 if TD_resp_grade__8==.
	replace TD_resp_grade__9 = TDs07_07__9 if TD_resp_grade__9==.
	replace TD_resp_grade__11 = TDs07_07__11 if TD_resp_grade__11==.
	replace TD_resp_grade__22 = TDs07_07__12 if TD_resp_grade__22==.
	replace TD_resp_grade__33 = TDs07_07__13 if TD_resp_grade__33==.

	drop 	TDs07_07__1 TDs07_07__2 TDs07_07__3 TDs07_07__4 TDs07_07__5 	///
			TDs07_07__6 TDs07_07__7 TDs07_07__8 TDs07_07__9 TDs07_07__11 	///
			TDs07_07__12 TDs07_07__13 TDs07_07__0

	replace TD03_11__0 = TDs07_08__0 if TD03_11__0==.
	replace TD03_11__1 = TDs07_08__1 if TD03_11__1==.
	replace TD03_11__2 = TDs07_08__2 if TD03_11__2==.
	replace TD03_11__3 = TDs07_08__3 if TD03_11__3==.
	replace TD03_11__4 = TDs07_08__4 if TD03_11__4==.
	replace TD03_11__5 = TDs07_08__5 if TD03_11__5==.
	replace TD03_11__6 = TDs07_08__6 if TD03_11__6==.
	replace TD03_11__7 = TDs07_08__7 if TD03_11__7==.
	replace TD03_11__8 = TDs07_08__8 if TD03_11__8==.
	replace TD03_11__9 = TDs07_08__9 if TD03_11__9==.
	replace TD03_11__11 = TDs07_08__11 if TD03_11__11==.
	replace TD03_11__22 = TDs07_08__12 if TD03_11__22==.
	replace TD03_11__33 = TDs07_08__13 if TD03_11__33==.

	drop 	TDs07_08__1 TDs07_08__2 TDs07_08__3 TDs07_08__4 TDs07_08__5 	///
			TDs07_08__6 TDs07_08__7 TDs07_08__8 TDs07_08__9 TDs07_08__11 	///
			TDs07_08__12 TDs07_08__13 TDs07_08__0

	// Education

	order TDs07_09, after(TD_edu)

	rename TDs07_09 TDs_edu
	lab var TDs_edu "(SCHOOL different value label) Highest level of education enjoyed by respondent"

	// Salary

	order TDs07_10, after(TD_salary)

	rename TDs07_10 TDs_salary
	lab var TDs_salary "(SCHOOL) Monthly salary"

	// Absentism last two weeks

	order TDs07_11, after(TD05_05_01)

	rename TDs07_11 TDs05_05_01
	lab var TDs05_05_01 "(SCHOOL) Days that teacher was absent last two weeks"

	// Absentism November last year

	order TDs07_12, after(TD05_08_01)

	rename TDs07_12 TDs05_08_01
	lab var TDs05_08_01 "(SCHOOL) Days that teacher was absent in November 2017"

	//

	order TDs07_04, after(TD_resp_pos)

	//

	order 	Ntd_date_time_end Ntd_sssys_irnd Ntd_has__errors 				///
			Ntd_interview__status Ntd_phone, after(Ntd_date_time_start)

	//

	order	Gp_parent_id3 Gp_school_name Gp_orange_id Gp_parent_name 		///
			Gp_parent_id Gp_date Gp_month Gp_leader1 Gp_leader2_1 			///
			Gp_leader2_2 Gp_blue Gp_orange Gp_version Gp_dg_faces0 			///
			Gp_dg_faces500 Gp_dg_faces1000 Gp_dg_faces1500 Gp_dg_faces2000 	///
			Gp_dg_belief0 Gp_dg_belief500 Gp_dg_belief1000 Gp_dg_belief1500 ///
			Gp_dg_belief2000 Gp_tg Gp_pgg Gp_pgg_belief Gp_pgg_belief_old 	///
			Gp_phone Gp_mismatch Np_interview__key Np_interview__id 		///
			Np_school_id Np_jogo_id_orange Np_teamleader Np_interviewer 	///
			Np_date_time_start Np_date_time_end Np_sssys_irnd 				///
			Np_has__errors Np_interview__status Np01 Np02 Np03_01 Np04_01 	///
			Np04_01_other Np03_02 Np04_02 Np04_02_other Np03_03 Np04_03	 	///
			Np04_03_other Np03_04 Np04_04 Np04_04_other Np03_05 Np04_05 	///
			Np04_05_other Np03_06 Np04_06 Np04_06_other Np03_07 Np04_07 	///
			Np04_07_other Np03_08 Np04_08 Np04_08_other Np03_09 Np04_09 	///
			Np04_09_other Np05 Np06 Np07_01 Np08_01 Np08_01_other Np07_02 	///
			Np08_02 Np08_02_other Np07_03 Np08_03 Np08_03_other Np07_04 	///
			Np08_04 Np08_04_other Np07_05 Np08_05 Np08_05_other, 			///
			after(TD15_13_20)

	// Created many duplicates when appending the two data sets.

	duplicates drop

	// Still some left: those that had only pt or mat tests

		// Parents

		sort school_id parent_id test_type

		by school_id parent_id: gen n = _n

		drop if test_type==. & n==2

		// Teachers

		sort school_id teacher_id test_type

		by school_id teacher_id: gen m = _n

		drop merge_Gp_Np merge_Sp_GNp merge_SGNp_Rpt merge_parents 			///
		merge_Gdt_Ndt merge_Sdt_GNdt merge_Std_Rst merge_teachers n m

replace teacher_id = 98 if TDs_teach_this_school_years==15 & teacher_id==99

merge m:1 school_id parent_id using "$Endline_dtRaw/_intermediate files/studentattendance.dta"
drop _merge

merge m:1 school_id teacher_id using "$Endline_dtRaw/_intermediate files/teacherattendance.dta"
drop _merge


// Merging with school level variables

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/director.dta"
drop _merge

foreach var in ss_key ss_id ss_random ss_errors survey_type game resp_id 	///
supervisor interviewer date_time_start date_time_end resp_name resp_tel 	///
resp_pos resp_teach resp_grade__0 resp_grade__1 resp_grade__2 resp_grade__3 ///
resp_grade__4 resp_grade__5 resp_grade__6 resp_grade__7 resp_grade__8 		///
resp_grade__9 resp_grade__11 resp_grade__22 resp_grade__33{
rename `var' D_`var'
}

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/school.dta"
drop _merge

foreach var in ss_key ss_id ss_random ss_errors school_name supervisor 		///
interviewer date_time_start date_time_end school_tel resp_name 				///
resp_position resp_tel resp_name_other_1 resp_position_other_1 				///
resp_name_other_2 resp_position_other_2 resp_name_other_3 					///
resp_position_other_3 resp_total community_type community_size 				///
school_distance{
rename `var' S_`var'
}

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/facilities.dta"
drop _merge

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/fda.dta"
drop _merge

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/admin.dta"
drop _merge

merge m:1 school_id using "$Endline_dtRaw/other/clusters.dta"
drop _merge

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/fda.dta"
drop _merge

merge m:1 school_id using "$Baseline_dtDeID/data_scorecard.dta"

drop _merge

merge m:1 school_id using "$Endline_dtRaw/_intermediate files/gps_urban.dta"

drop _merge

drop teacher_id2 TD_municipality TD_treatment

gen parent_id2 = 100*school_id + parent_id
gen teacher_id2 = 100*school_id + teacher_id

save "$Endline_dtRaw/_intermediate files/data_identified.dta", replace


// deidentifying the data

	drop ///
	/// Names
	P_resp_name P_student_name P_nome P02_06 P02_08 P02_10 P03_34 Ptd_teacher_name Ptd_student_name Ptd_teacher_name_lastyear ///
	TD_resp_name TD15_04* Np03* Np07* Ntd03* Ntd07* D15_04* ///
	D_resp_name S_resp_name S_resp_name_other_1 S_resp_name_other_2 S_resp_name_other_3 ///
	S08_02* S09_11* I_resp_name Gp_parent_name Gtd_teacher_name ///
	/// Phone numbers
	P_resp_tel TD_resp_tel D_resp_tel S_school_tel S_resp_tel I_resp_tel


	save "$Endline_dtDeID/data_deidentified.dta", replace




	/* 	All variable and value labels are in English. The original questions or
		other sources can be easily tracked down using the variable name.
		Letters indicate the source:

		Individual level:

		(test)	Student tests
		(P) 	Parents survey
		(D)		Director survey
		(T)		Teachers survey
		(TD)	Teachers and directors survey
		(Gp)	Games parents
		(Gtd)	Games teachers/directors
		(Np)	Network survey parents
		(Ntd)	Network survey teachers/directors

		School level:

		(D)		School directors survey
		(S)		School (admin) survey
		(I)		Infrastructure/facilities survey
		(FdA)	Our registration form with information and donations
		(A)		Admin data from provincial directorate

		Two special cases:

		(Ptd) 	Teachers anwered some questions about the parents of the
				selected students. These questions are included for each
				parents as Ptd after all other P variables.

		(TDs) 	In the school survey there are questions about all teachers.
				These questions are included for each teacher as TDs next to
				the TD variable related to it.


		Variables are numbers as follows: First number is the section, second
		number is the question and the optional third number indicates the
		choice option for a multiple choice question.

		The section numbering for the combined TD survey variables is according
		to the director survey. Section 1,2 and 9 do not have numbers anymore
		to facilitate the merging of these variables. However, using the labels
		it should not be difficult to track down the source question.



		The network survey does not have sections, so this number is omitted.

		Game variables are straightforward.


		IDENTIFICATION VARIABLES and TREATMENT INDICATOR:

		school_id teacher_id parent_id municipality treatment

		exclude_nonteachers: to exclude director that do not teach from the
		teacher variables. These should not be excluded when analysing the games.
